﻿/*
 * @(#)CodeWriteTag.java 1.0 2005-3-31
 * Copyright 2005 VandaGroup, Inc. All rights reserved.
 */
package com.vandagroup.twpos.taglib;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;

import org.apache.struts.taglib.html.TextTag;
import org.apache.struts.taglib.nested.NestedNameSupport;
import org.apache.struts.taglib.nested.NestedPropertyHelper;
import org.apache.struts.util.RequestUtils;


import com.vanda.db.cache.CodeHelper;
import com.vanda.db.cache.CodeInfo;
import com.vanda.util.LogFunc;

/**
 * 类说明:   
 * 代码表标签。根据ID值，在页面上写Code/Name值。
 * @author 王瑛<br>
 * @version Version 1.00<br>
 */
public class CodeWriteTag extends TextTag implements NestedNameSupport {
	/** 代码表名字 */
	private String codeName; 
	
	/** 代码表的值 */
	private String codeValue;
	
	/**
	 * 标签开始
	 */
	public int doStartTag() throws JspException {
		try {
			// request
			HttpServletRequest request =
				(HttpServletRequest) pageContext.getRequest();
			// set the properties
			NestedPropertyHelper.setNestedProperties(request, this);
			Object _defaultValue = null;
			if (property != null) {
				_defaultValue =
					RequestUtils.lookup(pageContext, name, property, null);
			}
			if (_defaultValue != null && !(_defaultValue instanceof String)) {
				_defaultValue = _defaultValue.toString();
			}
			StringBuffer html = new StringBuffer();
			if (_defaultValue != null) {

				CodeInfo _codeInfo = CodeHelper.getCode(codeName);
				ArrayList _list = _codeInfo.getCodeList();
				Integer index = (Integer) _codeInfo.getCodeIndex().get(_codeInfo.getKey().toLowerCase());
				if(index != null){
					int _keyIndex = index.intValue();
					Integer valueIndex = (Integer)_codeInfo.getCodeIndex().get(codeValue.toLowerCase());
					if(valueIndex != null){
						int _valueIndex = valueIndex.intValue();
						StringBuffer _values = new StringBuffer();
						for (int i = 0; i < _list.size(); i++) {
							ArrayList _item = (ArrayList) _list.get(i);
							_values.append(_item.get(_valueIndex));
							_values.setLength(_values.length());
							Object _key = _item.get(_keyIndex);
							if (_defaultValue != null && _defaultValue.equals(_key)) {
								html.append(_values);
								break;
							}
							_values.setLength(0);
						}

					}
				}
			}
			try {

				pageContext.getOut().write(html.toString());
			} catch (Exception e) {
				e.printStackTrace();
				throw new JspException("write error!");
			}
		} catch (JspException e) {
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
			LogFunc.info("codeName:" + codeName);
			LogFunc.info("codeValue:" + codeValue);
			LogFunc.info("property:" + property);
			LogFunc.info(
				"propertyValue:"
					+ RequestUtils.lookup(pageContext, name, property, null));
			throw new JspException("other error!" + e.getMessage());
		}
		return EVAL_BODY_INCLUDE;
	}

	public String getCodeName() {
		return codeName;
	}

	public String getCodeValue() {
		return codeValue;
	}

	public void setCodeName(String string) {
		codeName = string;
	}

	public void setCodeValue(String string) {
		codeValue = string;
	}
}